import os
import time
from common.setting import ensure_path_sep
from utils.logging_tool.log_control import LogHandler
from utils.mysqlUtils.db_pool import Database

current_path = os.path.abspath(__file__)
current_dir = os.path.dirname(current_path)
path_list = current_path.split("\\")
dis_path = "\\".join(path_list[-2:-1])


def remove_file(info_path):
    if os.path.exists(info_path):
        os.remove(info_path)
    else:
        print(f"{info_path} 不存在")


info_path = ensure_path_sep(f"\\logs\\info-{dis_path}{time.time()}")
error_path = ensure_path_sep(f"\\logs\\\error-{dis_path}{time.time()}.log")
warning_path = ensure_path_sep(f"\\logs\\warning-{dis_path}{time.time()}.log")
remove_file(info_path)
remove_file(error_path)
remove_file(warning_path)
INFO = LogHandler(info_path, level='info')
ERROR = LogHandler(error_path, level='error')
WARNING = LogHandler(warning_path, level='warning')


class PublishStatus:
    db = Database(ensure_path_sep("\\config\\shopeev2_config.yaml.bak"), "test_jdw_database")

    def get_source_sku(self, source_id):
        #获取datasyc表中渠道sku及发布状态
        sql1 = """
        SELECT source_id,sku,publish_status FROM test_zhcxkj_center_datasync_comprehensive.`t_jdw_full_product`  WHERE source_id=%s
        """
        res1 = self.db.fetch_all_tuple(sql1, (source_id,))
        listing_source_sku= set(map(lambda x: x[1], res1))
        print(listing_source_sku)
        data = list(filter(self.get_source_sku_status,listing_source_sku))
        print(data)
        print(len(data))


    def get_source_sku_status(self, sku):

        # 查询datasyc表中渠道sku的发布状态
        sql2 = """
                SELECT source_id,sku,publish_status FROM test_zhcxkj_center_datasync_comprehensive.`t_jdw_full_product`  WHERE  source_id=20395 AND sku=%s
                """
        # 查询listing表中渠道sku的发布状态
        sql3 = """
                SELECT source_id,source_sku,`publish_status` FROM test_zhcxkj_center_listing_comprehensive.`t_jdwalmart_listing` WHERE source_id=20395 AND source_sku=%s
                """
        res2 = self.db.fetch_all_tuple(sql2, (sku,))
        res3 = self.db.fetch_all_tuple(sql3, (sku,))
        if not res3:
            return
        datasyc_status = list((map(lambda x: x[2], res2)))
        listing_status = list((map(lambda x: x[2], res3)))
        if listing_status[0]==1:
            listing_status=['PUBLISHED']
        elif listing_status[0]==2:
            listing_status=['READY_TO_PUBLISH']
        elif listing_status[0]==3:
            listing_status=['IN_PROGRESS']
        elif listing_status[0]==4:
            listing_status=['UNPUBLISHED']
        elif listing_status[0]==5:
            listing_status=['STAGE']
        else:
            listing_status=['SYSTEM_PROBLEM']

        if datasyc_status == listing_status:
            INFO.logger.info(f"sku={sku}状态相同")
            return False
        else:
            INFO.logger.info(f"sku={sku}状态不同")
            return True



if __name__ == '__main__':
    PublishStatus().get_source_sku(20395)
    # PublishStatus().get_source_sku_status('1056852930')